// facility/map/map.js
import http from '../../http'
import gcoord from 'gcoord'
import {
	handleTree
} from "../../libs/tool";
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		top: '',
		left: '',
		height: '',
		width: '',

		projectBol: false,
		projectInfo: {}, //跳转的有chilren的项目信息

		markers: [],
		latitude: '',
		longitude: '',
		scale: 18,
		rgcData: {},

		nowProjectInfo: {}, //当前项目(分组)信息
		nowfacilityInfo: {}, // 当前灯杆信息
		infoType: 'project', // 判断当前显示项目还是灯杆

		facilityType: '',
		facilityTypeId: '',
		facilityTypeShow: false,
		facilityTypeList: [],

		addFacilityBol: false,
		addType: '1',
	},
	markerArray(arr) {
		let list = arr.map(item => {
			return {
				longitude: this.coord([item.longitude, item.latitude])[0],
				latitude: this.coord([item.longitude, item.latitude])[1],
				iconPath: item.typeId == 2 ? '../../img/map/power-box.png' : '../../img/map/lamp-post.png',
				width: 20,
				height: 20,
				id: Number(item.id),
				typeId: item.typeId,
				name: item.name,
				sn: item.sn,
				zIndex: 100,
				callout: {
					display: 'BYCLICK',
					content: item.name,
					padding: 5,
					borderRadius: 10
				},
			}
		})
		console.log(list);
		return list
	},
	coord(location) {
		var result = gcoord.transform(
			location, // 经纬度坐标
			gcoord.BD09, // 当前坐标系
			gcoord.GCJ02 // 目标坐标系
		);
		return result
	},
	// 返回
	back() {
		wx.navigateBack({
			delta: 1
		});
	},
	// 搜索
	searchBtn() {
		if (this.data.search.trim() != '') {
			let list = this.data.markers.filter(item => {
				return item.sn == this.data.search.trim()
			})
			if (list.length != 0) {
				this.setData({
					latitude: list[0].latitude,
					longitude: list[0].longitude,
					scale: 22,
				})
			} else {
				wx.showToast({
					title: '项目下没有该灯杆',
					icon: 'error',
				})
			}
		}


	},
	// 点击maker
	makertap(value) {
		console.log(value);
		http.get({
			url: '/zg/light-pole/get',
			data: {
				id: value.markerId,
			},
			then: (res) => {
				console.log('灯杆信息', res);
				this.setData({
					nowfacilityInfo: res.data.data,
					infoType: 'facility'
				})
			}
		})
	},
	// 项目选择
	projectShow() {
		this.setData({
			projectBol: true
		});
	},
	projectClose() {
		this.setData({
			projectBol: false
		});
	},
	projectCut(event) {
		wx.showLoading({
			title: '加载中...'
		});
		console.log(event.currentTarget.dataset);
		let data = event.currentTarget.dataset
		this.setData({
			nowProjectInfo: {
				name: data.info.projectName,
				id: data.info.id,
				type: data.info.parentId
			},
			longitude: this.coord([data.info.longitude, data.info.latitude])[0],
			latitude: this.coord([data.info.longitude, data.info.latitude])[1],
			scale: 18,
			projectBol: false,
			infoType: 'project'
		}, () => {
			let pages = getCurrentPages()
			let currentPage = pages[pages.length - 1]
			let options = currentPage.options
			options.projectId = data.info.id
			this.gain()
		})
	},
	// 定位自身位置
	selfPositioning() {
		wx.getLocation({
			type: "gcj02",
			success: (res) => {
				console.log(res);
				this.setData({
					longitude: res.longitude,
					latitude: res.latitude
				})
			},
			fail(error) {
				console.log(error);
			}
		})
	},
	// 新增载体
	addLamp() {
		console.log(this.data.nowProjectInfo);
		wx.navigateTo({
			url: `../addLamp/addLamp?projectId=${this.data.nowProjectInfo.id}`,
		})
	},
	// 新增设备
	addFacility() {
		if (this.data.infoType != 'facility') {
			wx.showToast({
				title: '请先选择灯杆',
				icon: 'error',
			})
		} else {
			wx.navigateTo({
				url: `../addFacility/addFacility?lampId=${this.data.nowfacilityInfo.id}`,
			})
			// this.setData({
			// 	addFacilityBol: true
			// })
		}
	},
	addFacilityNext() {
		wx.navigateTo({
			url: `../addFacility/addFacility?lampId=${this.data.nowfacilityInfo.id}`,
		})
		// if (this.data.addType == 1) {
		// 	wx.navigateTo({
		// 		url: `../addFacility/addFacility?lampId=${this.data.nowfacilityInfo.id}`,
		// 	})
		// } else {
		// 	wx.scanCode({
		// 		scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
		// 		success: (res) => {
		// 			console.log('二维码数据', res)
		// 			wx.navigateTo({
		// 				url: `../addFacility/addFacility?facilitySn=${res.result}&lampId=${this.data.nowfacilityInfo.id}`,
		// 			})
		// 		}
		// 	})
		// }
	},
	addFacilityClose() {
		this.setData({
			addFacilityBol: false
		})
	},

	addTypeChange(event) {
		this.setData({
			addType: event.detail,
		});
	},
	gain() {
		let params = {
			pageNo: 1,
			pageSize: -1,
			categoryId: '',
		}
		if (this.data.nowProjectInfo.type == 0) {
			params.projectId = this.data.nowProjectInfo.id
		} else {
			params.group = this.data.nowProjectInfo.id
		}
		http.get({
			url: '/zg/light-pole/mapPage',
			data: params,
			then: (res) => {
				console.log('灯杆信息', res);
				this.setData({
						markers: this.markerArray(res.data.data),
					},
					() => {
						wx.hideLoading()
					})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(options);

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		wx.showLoading({
			title: '加载中...'
		});
		this.setData({
			top: wx.getStorageSync('customTop'),
			left: wx.getStorageSync('screenWidth') - wx.getStorageSync('customRight'),
			height: wx.getStorageSync('customHeight'),
			width: wx.getStorageSync('customLeft'),
		})

		let pages = getCurrentPages()
		console.log(pages);
		let currentPage = pages[pages.length - 1]
		let options = currentPage.options
		http.get({
			url: '/zg/project/get',
			data: {
				id: options.projectId,
			},
			then: (res) => {
				console.log('项目(分组)信息', res);
				this.setData({
					nowProjectInfo: {
						name: res.data.data.projectName,
						id: res.data.data.id,
						type: res.data.data.parentId,
					},
					longitude: this.coord([res.data.data.longitude, res.data.data.latitude])[0],
					latitude: this.coord([res.data.data.longitude, res.data.data.latitude])[1],
				})
				http.get({
					url: '/zg/project/list',
					data: {
						countType: 'lightpole',
					},
					then: (Res) => {
						console.log('所有项目信息', Res);
						let list = handleTree(Res.data.data, "id", "parentId");
						list.forEach(item => {
							if (item.id == options.projectId) {
								console.log(item);
								this.setData({
									projectInfo: item
								})
							}
						})
						this.gain()
					}
				})
			}
		})


	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})